package middleware

import (
	"saas-app/app/dao"
	"saas-app/app/define"
	"saas-app/library/response"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func CampusAuth(r *ghttp.Request) {

	var (
		data *define.CheckCampusAuth
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	r.SetCtxVar("school_id", data.SchoolId)
	r.SetCtxVar("campus_id", data.CampusId)

	userId := r.GetCtxVar("user_id").Uint()

	record, err := dao.SchoolUser.Where("school_id = ?", data.SchoolId).And("user_id = ?", userId).And("campus_id", g.Slice{data.CampusId, 0}).FindOne()

	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if record == nil {
		response.JsonExit(r, 110, "无权限")
	}

	r.Middleware.Next()
}
